home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / matrix.c < prev    next >
C/C++ Source or Header  |  1999-02-04  |  28KB  |  982 lines

  1. /* $Id: matrix.c,v 3.7 1998/08/23 22:18:18 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: matrix.c,v $
  26.  * Revision 3.7  1998/08/23 22:18:18  brianp
  27.  * added Driver.Viewport and Driver.DepthRange function pointers
  28.  *
  29.  * Revision 3.6  1998/06/07 22:18:52  brianp
  30.  * implemented GL_EXT_multitexture extension
  31.  *
  32.  * Revision 3.5  1998/05/07 00:12:57  brianp
  33.  * new invert_matrix() function from Jacques Leroy
  34.  *
  35.  * Revision 3.4  1998/03/27 04:17:31  brianp
  36.  * fixed G++ warnings
  37.  *
  38.  * Revision 3.3  1998/03/03 02:39:29  brianp
  39.  * fixed divide by zero problem in gl_LoadMatrixf() (Tim Rowley)
  40.  *
  41.  * Revision 3.2  1998/02/20 04:50:44  brianp
  42.  * implemented GL_SGIS_multitexture
  43.  *
  44.  * Revision 3.1  1998/02/08 20:19:12  brianp
  45.  * removed unneeded headers
  46.  *
  47.  * Revision 3.0  1998/01/31 20:58:46  brianp
  48.  * initial rev
  49.  *
  50.  */
  51.  
  52.  
  53. /*
  54.  * Matrix operations
  55.  *
  56.  *
  57.  * NOTES:
  58.  * 1. 4x4 transformation matrices are stored in memory in column major order.
  59.  * 2. Points/vertices are to be thought of as column vectors.
  60.  * 3. Transformation of a point p by a matrix M is: p' = M * p
  61.  *
  62.  */
  63.  
  64.  
  65. #ifdef PC_HEADER
  66. #include "all.h"
  67. #else
  68. #include <math.h>
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include "context.h"
  73. #include "macros.h"
  74. #include "matrix.h"
  75. #include "mmath.h"
  76. #include "types.h"
  77. #endif
  78.  
  79.  
  80.  
  81. static GLfloat Identity[16] = {
  82.    1.0, 0.0, 0.0, 0.0,
  83.    0.0, 1.0, 0.0, 0.0,
  84.    0.0, 0.0, 1.0, 0.0,
  85.    0.0, 0.0, 0.0, 1.0
  86. };
  87.  
  88.  
  89.  
  90.  
  91. #if 0
  92. static void print_matrix( const GLfloat m[16] )
  93. {
  94.    int i;
  95.  
  96.    for (i=0;i<4;i++) {
  97.       printf("%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
  98.    }
  99. }
  100. #endif
  101.  
  102.  
  103.  
  104. /*
  105.  * Perform a 4x4 matrix multiplication  (product = a x b).
  106.  * Input:  a, b - matrices to multiply
  107.  * Output:  product - product of a and b
  108.  * WARNING: (product != b) assumed
  109.  * NOTE:    (product == a) allowed    
  110.  */
  111. static void matmul( GLfloat *product, const GLfloat *a, const GLfloat *b )
  112. {
  113.    /* This matmul was contributed by Thomas Malik */
  114.    GLint i;
  115.  
  116. #define A(row,col)  a[(col<<2)+row]
  117. #define B(row,col)  b[(col<<2)+row]
  118. #define P(row,col)  product[(col<<2)+row]
  119.  
  120.    /* i-te Zeile */
  121.    for (i = 0; i < 4; i++) {
  122.       GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
  123.       P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
  124.       P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
  125.       P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
  126.       P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
  127.    }
  128.  
  129. #undef A
  130. #undef B
  131. #undef P
  132. }
  133.  
  134.  
  135.  
  136. /*
  137.  * Compute inverse of 4x4 transformation matrix.
  138.  * Code contributed by Jacques Leroy jle@star.be
  139.  * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
  140.  */
  141. static GLboolean invert_matrix( const GLfloat *m, GLfloat *out )
  142. {
  143. /* NB. OpenGL Matrices are COLUMN major. */
  144. #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
  145. #define MAT(m,r,c) (m)[(c)*4+(r)]
  146.  
  147.  GLfloat wtmp[4][8];
  148.  GLfloat m0, m1, m2, m3, s;
  149.  GLfloat *r0, *r1, *r2, *r3;
  150.  
  151.  r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
  152.  
  153.  r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
  154.  r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
  155.  r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
  156.  
  157.  r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
  158.  r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
  159.  r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
  160.  
  161.  r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
  162.  r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
  163.  r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
  164.  
  165.  r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
  166.  r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
  167.  r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
  168.  
  169.  /* choose pivot - or die */
  170.  if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
  171.  if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
  172.  if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
  173.  if (0.0 == r0[0])  return GL_FALSE;
  174.  
  175.  /* eliminate first variable     */
  176.  m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
  177.  s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
  178.  s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
  179.  s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
  180.  s = r0[4];
  181.  if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
  182.  s = r0[5];
  183.  if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
  184.  s = r0[6];
  185.  if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
  186.  s = r0[7];
  187.  if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
  188.  
  189.  /* choose pivot - or die */
  190.  if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
  191.  if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
  192.  if (0.0 == r1[1])  return GL_FALSE;
  193.  
  194.  /* eliminate second variable */
  195.  m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
  196.  r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
  197.  r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
  198.  s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
  199.  s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
  200.  s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
  201.  s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
  202.  
  203.  /* choose pivot - or die */
  204.  if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
  205.  if (0.0 == r2[2])  return GL_FALSE;
  206.  
  207.  /* eliminate third variable */
  208.  m3 = r3[2]/r2[2];
  209.  r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
  210.  r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
  211.  r3[7] -= m3 * r2[7];
  212.  
  213.  /* last check */
  214.  if (0.0 == r3[3]) return GL_FALSE;
  215.  
  216.  s = 1.0/r3[3];              /* now back substitute row 3 */
  217.  r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
  218.  
  219.  m2 = r2[3];                 /* now back substitute row 2 */
  220.  s  = 1.0/r2[2];
  221.  r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
  222.  r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
  223.  m1 = r1[3];
  224.  r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
  225.  r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
  226.  m0 = r0[3];
  227.  r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
  228.  r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
  229.  
  230.  m1 = r1[2];                 /* now back substitute row 1 */
  231.  s  = 1.0/r1[1];
  232.  r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
  233.  r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
  234.  m0 = r0[2];
  235.  r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
  236.  r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
  237.  
  238.  m0 = r0[1];                 /* now back substitute row 0 */
  239.  s  = 1.0/r0[0];
  240.  r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
  241.  r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
  242.  
  243.  MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
  244.  MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
  245.  MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
  246.  MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
  247.  MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
  248.  MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
  249.  MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
  250.  MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; 
  251.  
  252.  return GL_TRUE;
  253.  
  254. #undef MAT
  255. #undef SWAP_ROWS
  256. }
  257.  
  258.  
  259.  
  260. /*
  261.  * Determine if the given matrix is the identity matrix.
  262.  */
  263. static GLboolean is_identity( const GLfloat m[16] )
  264. {
  265.    if (   m[0]==1.0F && m[4]==0.0F && m[ 8]==0.0F && m[12]==0.0F
  266.        && m[1]==0.0F && m[5]==1.0F && m[ 9]==0.0F && m[13]==0.0F
  267.        && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F
  268.        && m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) {
  269.       return GL_TRUE;
  270.    }
  271.    else {
  272.       return GL_FALSE;
  273.    }
  274. }
  275.  
  276.  
  277. /*
  278.  * Examine the current modelview matrix to determine its type.
  279.  * Later we use the matrix type to optimize vertex transformations.
  280.  */
  281. void gl_analyze_modelview_matrix( GLcontext *ctx )
  282. {
  283.    const GLfloat *m = ctx->ModelViewMatrix;
  284.    if (is_identity(m)) {
  285.       ctx->ModelViewMatrixType = MATRIX_IDENTITY;
  286.    }
  287.    else if (                 m[4]==0.0F && m[ 8]==0.0F               
  288.         && m[1]==0.0F               && m[ 9]==0.0F
  289.         && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F
  290.         && m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) {
  291.       ctx->ModelViewMatrixType = MATRIX_2D_NO_ROT;
  292.    }
  293.    else if (                               m[ 8]==0.0F               
  294.         &&                             m[ 9]==0.0F
  295.         && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F
  296.         && m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) {
  297.       ctx->ModelViewMatrixType = MATRIX_2D;
  298.    }
  299.    else if (m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) {
  300.       ctx->ModelViewMatrixType = MATRIX_3D;
  301.    }
  302.    else {
  303.       ctx->ModelViewMatrixType = MATRIX_GENERAL;
  304.    }
  305.  
  306.    if (!invert_matrix( ctx->ModelViewMatrix, ctx->ModelViewInv )) {
  307.       MEMCPY( ctx->ModelViewInv, Identity, 16 * sizeof(GLfloat) );
  308.    }
  309.  
  310.    ctx->NewModelViewMatrix = GL_FALSE;
  311. }
  312.  
  313.  
  314.  
  315. /*
  316.  * Examine the current projection matrix to determine its type.
  317.  * Later we use the matrix type to optimize vertex transformations.
  318.  */
  319. void gl_analyze_projection_matrix( GLcontext *ctx )
  320. {
  321.    /* look for common-case ortho and perspective matrices */
  322.    const GLfloat *m = ctx->ProjectionMatrix;
  323.    if (is_identity(m)) {
  324.       ctx->ProjectionMatrixType = MATRIX_IDENTITY;
  325.    }
  326.    else if (                 m[4]==0.0F && m[8] ==0.0F
  327.         && m[1]==0.0F               && m[9] ==0.0F
  328.         && m[2]==0.0F && m[6]==0.0F
  329.         && m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) {
  330.       ctx->ProjectionMatrixType = MATRIX_ORTHO;
  331.    }
  332.    else if (                 m[4]==0.0F                 && m[12]==0.0F
  333.         && m[1]==0.0F                               && m[13]==0.0F
  334.         && m[2]==0.0F && m[6]==0.0F
  335.         && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
  336.       ctx->ProjectionMatrixType = MATRIX_PERSPECTIVE;
  337.    }
  338.    else {
  339.       ctx->ProjectionMatrixType = MATRIX_GENERAL;
  340.    }
  341.  
  342.    ctx->NewProjectionMatrix = GL_FALSE;
  343. }
  344.  
  345.  
  346.  
  347. /*
  348.  * Examine the current texture matrix to determine its type.
  349.  * Later we use the matrix type to optimize texture coordinate transformations.
  350.  */
  351. void gl_analyze_texture_matrix( GLcontext *ctx )
  352. {
  353.    GLuint texSet;
  354.    for (texSet = 0; texSet<MAX_TEX_COORD_SETS; texSet++) {
  355.       const GLfloat *m = ctx->TextureMatrix[texSet];
  356.       if (is_identity(m)) {
  357.      ctx->TextureMatrixType[texSet] = MATRIX_IDENTITY;
  358.       }
  359.       else if (                               m[ 8]==0.0F               
  360.            &&                             m[ 9]==0.0F
  361.            && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F
  362.            && m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) {
  363.      ctx->TextureMatrixType[texSet] = MATRIX_2D;
  364.       }
  365.       else if (m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) {
  366.      ctx->TextureMatrixType[texSet] = MATRIX_3D;
  367.       }
  368.       else {
  369.      ctx->TextureMatrixType[texSet] = MATRIX_GENERAL;
  370.       }
  371.    }
  372.    ctx->NewTextureMatrix = GL_FALSE;
  373. }
  374.  
  375.  
  376.  
  377. void gl_Frustum( GLcontext *ctx,
  378.          GLdouble left, GLdouble right,
  379.          GLdouble bottom, GLdouble top,
  380.          GLdouble nearval, GLdouble farval )
  381. {
  382.    GLfloat x, y, a, b, c, d;
  383.    GLfloat m[16];
  384.  
  385.    if (nearval<=0.0 || farval<=0.0) {
  386.       gl_error( ctx,  GL_INVALID_VALUE, "glFrustum(near or far)" );
  387.    }
  388.  
  389.    x = (2.0*nearval) / (right-left);
  390.    y = (2.0*nearval) / (top-bottom);
  391.    a = (right+left) / (right-left);
  392.    b = (top+bottom) / (top-bottom);
  393.    c = -(farval+nearval) / ( farval-nearval);
  394.    d = -(2.0*farval*nearval) / (farval-nearval);  /* error? */
  395.  
  396. #define M(row,col)  m[col*4+row]
  397.    M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = a;      M(0,3) = 0.0F;
  398.    M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = b;      M(1,3) = 0.0F;
  399.    M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = c;      M(2,3) = d;
  400.    M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = -1.0F;  M(3,3) = 0.0F;
  401. #undef M
  402.  
  403.    gl_MultMatrixf( ctx, m );
  404.  
  405.  
  406.    /* Need to keep a stack of near/far values in case the user push/pops
  407.     * the projection matrix stack so that we can call Driver.NearFar()
  408.     * after a pop.
  409.     */
  410.    ctx->NearFarStack[ctx->ProjectionStackDepth][0] = nearval;
  411.    ctx->NearFarStack[ctx->ProjectionStackDepth][1] = farval;
  412.  
  413.    if (ctx->Driver.NearFar) {
  414.       (*ctx->Driver.NearFar)( ctx, nearval, farval );
  415.    }
  416. }
  417.  
  418.  
  419. void gl_Ortho( GLcontext *ctx,
  420.            GLdouble left, GLdouble right,
  421.            GLdouble bottom, GLdouble top,
  422.            GLdouble nearval, GLdouble farval )
  423. {
  424.    GLfloat x, y, z;
  425.    GLfloat tx, ty, tz;
  426.    GLfloat m[16];
  427.  
  428.    x = 2.0 / (right-left);
  429.    y = 2.0 / (top-bottom);
  430.    z = -2.0 / (farval-nearval);
  431.    tx = -(right+left) / (right-left);
  432.    ty = -(top+bottom) / (top-bottom);
  433.    tz = -(farval+nearval) / (farval-nearval);
  434.  
  435. #define M(row,col)  m[col*4+row]
  436.    M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = 0.0F;  M(0,3) = tx;
  437.    M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = 0.0F;  M(1,3) = ty;
  438.    M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = z;     M(2,3) = tz;
  439.    M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = 0.0F;  M(3,3) = 1.0F;
  440. #undef M
  441.  
  442.    gl_MultMatrixf( ctx, m );
  443.  
  444.    if (ctx->Driver.NearFar) {
  445.       (*ctx->Driver.NearFar)( ctx, nearval, farval );
  446.    }
  447. }
  448.  
  449.  
  450. void gl_MatrixMode( GLcontext *ctx, GLenum mode )
  451. {
  452.    if (INSIDE_BEGIN_END(ctx)) {
  453.       gl_error( ctx,  GL_INVALID_OPERATION, "glMatrixMode" );
  454.       return;
  455.    }
  456.    switch (mode) {
  457.       case GL_MODELVIEW:
  458.       case GL_PROJECTION:
  459.       case GL_TEXTURE:
  460.      ctx->Transform.MatrixMode = mode;
  461.      break;
  462.       default:
  463.      gl_error( ctx,  GL_INVALID_ENUM, "glMatrixMode" );
  464.    }
  465. }
  466.  
  467.  
  468.  
  469. void gl_PushMatrix( GLcontext *ctx )
  470. {
  471.    if (INSIDE_BEGIN_END(ctx)) {
  472.       gl_error( ctx,  GL_INVALID_OPERATION, "glPushMatrix" );
  473.       return;
  474.    }
  475.    switch (ctx->Transform.MatrixMode) {
  476.       case GL_MODELVIEW:
  477.      if (ctx->ModelViewStackDepth>=MAX_MODELVIEW_STACK_DEPTH-1) {
  478.         gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix");
  479.         return;
  480.      }
  481.      MEMCPY( ctx->ModelViewStack[ctx->ModelViewStackDepth],
  482.          ctx->ModelViewMatrix,
  483.          16*sizeof(GLfloat) );
  484.      ctx->ModelViewStackDepth++;
  485.      break;
  486.       case GL_PROJECTION:
  487.      if (ctx->ProjectionStackDepth>=MAX_PROJECTION_STACK_DEPTH) {
  488.         gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix");
  489.         return;
  490.      }
  491.      MEMCPY( ctx->ProjectionStack[ctx->ProjectionStackDepth],
  492.          ctx->ProjectionMatrix,
  493.          16*sizeof(GLfloat) );
  494.      ctx->ProjectionStackDepth++;
  495.  
  496.      /* Save near and far projection values */
  497.      ctx->NearFarStack[ctx->ProjectionStackDepth][0]
  498.         = ctx->NearFarStack[ctx->ProjectionStackDepth-1][0];
  499.      ctx->NearFarStack[ctx->ProjectionStackDepth][1]
  500.         = ctx->NearFarStack[ctx->ProjectionStackDepth-1][1];
  501.      break;
  502.       case GL_TEXTURE:
  503.      {
  504.         GLuint texSet = ctx->Texture.CurrentTransformSet;
  505.         if (ctx->TextureStackDepth[texSet] >= MAX_TEXTURE_STACK_DEPTH) {
  506.            gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix");
  507.            return;
  508.         }
  509.         MEMCPY( ctx->TextureStack[texSet][ctx->TextureStackDepth[texSet]],
  510.             ctx->TextureMatrix[texSet],
  511.             16*sizeof(GLfloat) );
  512.         ctx->TextureStackDepth[texSet]++;
  513.      }
  514.      break;
  515.       default:
  516.      gl_problem(ctx, "Bad matrix mode in gl_PushMatrix");
  517.    }
  518. }
  519.  
  520.  
  521.  
  522. void gl_PopMatrix( GLcontext *ctx )
  523. {
  524.    if (INSIDE_BEGIN_END(ctx)) {
  525.       gl_error( ctx,  GL_INVALID_OPERATION, "glPopMatrix" );
  526.       return;
  527.    }
  528.    switch (ctx->Transform.MatrixMode) {
  529.       case GL_MODELVIEW:
  530.      if (ctx->ModelViewStackDepth==0) {
  531.         gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix");
  532.         return;
  533.      }
  534.      ctx->ModelViewStackDepth--;
  535.      MEMCPY( ctx->ModelViewMatrix,
  536.          ctx->ModelViewStack[ctx->ModelViewStackDepth],
  537.          16*sizeof(GLfloat) );
  538.      ctx->NewModelViewMatrix = GL_TRUE;
  539.      break;
  540.       case GL_PROJECTION:
  541.      if (ctx->ProjectionStackDepth==0) {
  542.         gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix");
  543.         return;
  544.      }
  545.      ctx->ProjectionStackDepth--;
  546.      MEMCPY( ctx->ProjectionMatrix,
  547.          ctx->ProjectionStack[ctx->ProjectionStackDepth],
  548.          16*sizeof(GLfloat) );
  549.      ctx->NewProjectionMatrix = GL_TRUE;
  550.  
  551.      /* Device driver near/far values */
  552.      {
  553.         GLfloat nearVal = ctx->NearFarStack[ctx->ProjectionStackDepth][0];
  554.         GLfloat farVal  = ctx->NearFarStack[ctx->ProjectionStackDepth][1];
  555.         if (ctx->Driver.NearFar) {
  556.            (*ctx->Driver.NearFar)( ctx, nearVal, farVal );
  557.         }
  558.      }
  559.      break;
  560.       case GL_TEXTURE:
  561.      {
  562.         GLuint texSet = ctx->Texture.CurrentTransformSet;
  563.         if (ctx->TextureStackDepth[texSet]==0) {
  564.            gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix");
  565.            return;
  566.         }
  567.         ctx->TextureStackDepth[texSet]--;
  568.         MEMCPY( ctx->TextureMatrix[texSet],
  569.             ctx->TextureStack[texSet][ctx->TextureStackDepth[texSet]],
  570.             16*sizeof(GLfloat) );
  571.         ctx->NewTextureMatrix = GL_TRUE;
  572.      }
  573.      break;
  574.       default:
  575.      gl_problem(ctx, "Bad matrix mode in gl_PopMatrix");
  576.    }
  577. }
  578.  
  579.  
  580.  
  581. void gl_LoadIdentity( GLcontext *ctx )
  582. {
  583.    if (INSIDE_BEGIN_END(ctx)) {
  584.       gl_error( ctx,  GL_INVALID_OPERATION, "glLoadIdentity" );
  585.       return;
  586.    }
  587.    switch (ctx->Transform.MatrixMode) {
  588.       case GL_MODELVIEW:
  589.      MEMCPY( ctx->ModelViewMatrix, Identity, 16*sizeof(GLfloat) );
  590.      MEMCPY( ctx->ModelViewInv, Identity, 16*sizeof(GLfloat) );
  591.      ctx->ModelViewMatrixType = MATRIX_IDENTITY;
  592.      ctx->NewModelViewMatrix = GL_FALSE;
  593.      break;
  594.       case GL_PROJECTION:
  595.      MEMCPY( ctx->ProjectionMatrix, Identity, 16*sizeof(GLfloat) );
  596.      ctx->ProjectionMatrixType = MATRIX_IDENTITY;
  597.      ctx->NewProjectionMatrix = GL_FALSE;
  598.      break;
  599.       case GL_TEXTURE:
  600.      {
  601.         GLuint texSet = ctx->Texture.CurrentTransformSet;
  602.         MEMCPY( ctx->TextureMatrix[texSet], Identity, 16*sizeof(GLfloat) );
  603.         ctx->TextureMatrixType[texSet] = MATRIX_IDENTITY;
  604.         ctx->NewTextureMatrix = GL_FALSE;
  605.      }
  606.      break;
  607.       default:
  608.      gl_problem(ctx, "Bad matrix mode in gl_LoadIdentity");
  609.    }
  610. }
  611.  
  612.  
  613. void gl_LoadMatrixf( GLcontext *ctx, const GLfloat *m )
  614. {
  615.    if (INSIDE_BEGIN_END(ctx)) {
  616.       gl_error( ctx,  GL_INVALID_OPERATION, "glLoadMatrix" );
  617.       return;
  618.    }
  619.    switch (ctx->Transform.MatrixMode) {
  620.       case GL_MODELVIEW:
  621.      MEMCPY( ctx->ModelViewMatrix, m, 16*sizeof(GLfloat) );
  622.      ctx->NewModelViewMatrix = GL_TRUE;
  623.      break;
  624.       case GL_PROJECTION:
  625.      MEMCPY( ctx->ProjectionMatrix, m, 16*sizeof(GLfloat) );
  626.      ctx->NewProjectionMatrix = GL_TRUE;
  627.      {
  628.         GLfloat n, f, c, d;
  629.  
  630. #define M(row,col)  m[col*4+row]
  631.         c = M(2,2);
  632.         d = M(2,3);
  633. #undef M
  634.         if (c == 1.0)
  635.            n = 0.0;
  636.         else
  637.            n = d / (c - 1.0);
  638.  
  639.         if (c == -1.0)
  640.            f = 1.0;
  641.         else
  642.            f = d / (c + 1.0);
  643.  
  644.         /* Need to keep a stack of near/far values in case the user
  645.          * push/pops the projection matrix stack so that we can call
  646.          * Driver.NearFar() after a pop.
  647.          */
  648.         ctx->NearFarStack[ctx->ProjectionStackDepth][0] = n;
  649.         ctx->NearFarStack[ctx->ProjectionStackDepth][1] = f;
  650.  
  651.         if (ctx->Driver.NearFar) {
  652.            (*ctx->Driver.NearFar)( ctx, n, f );
  653.         }
  654.      }
  655.      break;
  656.       case GL_TEXTURE:
  657.      {
  658.         GLuint texSet = ctx->Texture.CurrentTransformSet;
  659.         MEMCPY( ctx->TextureMatrix[texSet], m, 16*sizeof(GLfloat) );
  660.         ctx->NewTextureMatrix = GL_TRUE;
  661.      }
  662.      break;
  663.       default:
  664.      gl_problem(ctx, "Bad matrix mode in gl_LoadMatrixf");
  665.    }
  666. }
  667.  
  668.  
  669.  
  670. void gl_MultMatrixf( GLcontext *ctx, const GLfloat *m )
  671. {
  672.    if (INSIDE_BEGIN_END(ctx)) {
  673.       gl_error( ctx,  GL_INVALID_OPERATION, "glMultMatrix" );
  674.       return;
  675.    }
  676.    switch (ctx->Transform.MatrixMode) {
  677.       case GL_MODELVIEW:
  678.      matmul( ctx->ModelViewMatrix, ctx->ModelViewMatrix, m );
  679.      ctx->NewModelViewMatrix = GL_TRUE;
  680.      break;
  681.       case GL_PROJECTION:
  682.      matmul( ctx->ProjectionMatrix, ctx->ProjectionMatrix, m );
  683.      ctx->NewProjectionMatrix = GL_TRUE;
  684.      break;
  685.       case GL_TEXTURE:
  686.      {
  687.         GLuint texSet = ctx->Texture.CurrentTransformSet;
  688.         matmul( ctx->TextureMatrix[texSet], ctx->TextureMatrix[texSet], m );
  689.         ctx->NewTextureMatrix = GL_TRUE;
  690.      }
  691.      break;
  692.       default:
  693.      gl_problem(ctx, "Bad matrix mode in gl_MultMatrixf");
  694.    }
  695. }
  696.  
  697.  
  698.  
  699. /*
  700.  * Generate a 4x4 transformation matrix from glRotate parameters.
  701.  */
  702. void gl_rotation_matrix( GLfloat angle, GLfloat x, GLfloat y, GLfloat z,
  703.              GLfloat m[] )
  704. {
  705.    /* This function contributed by Erich Boleyn (erich@uruk.org) */
  706.    GLfloat mag, s, c;
  707.    GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
  708.  
  709.    s = sin( angle * DEG2RAD );
  710.    c = cos( angle * DEG2RAD );
  711.  
  712.    mag = GL_SQRT( x*x + y*y + z*z );
  713.  
  714.    if (mag == 0.0) {
  715.       /* generate an identity matrix and return */
  716.       MEMCPY(m, Identity, sizeof(GLfloat)*16);
  717.       return;
  718.    }
  719.  
  720.    x /= mag;
  721.    y /= mag;
  722.    z /= mag;
  723.  
  724. #define M(row,col)  m[col*4+row]
  725.  
  726.    /*
  727.     *     Arbitrary axis rotation matrix.
  728.     *
  729.     *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
  730.     *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
  731.     *  (which is about the X-axis), and the two composite transforms
  732.     *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
  733.     *  from the arbitrary axis to the X-axis then back.  They are
  734.     *  all elementary rotations.
  735.     *
  736.     *  Rz' is a rotation about the Z-axis, to bring the axis vector
  737.     *  into the x-z plane.  Then Ry' is applied, rotating about the
  738.     *  Y-axis to bring the axis vector parallel with the X-axis.  The
  739.     *  rotation about the X-axis is then performed.  Ry and Rz are
  740.     *  simply the respective inverse transforms to bring the arbitrary
  741.     *  axis back to it's original orientation.  The first transforms
  742.     *  Rz' and Ry' are considered inverses, since the data from the
  743.     *  arbitrary axis gives you info on how to get to it, not how
  744.     *  to get away from it, and an inverse must be applied.
  745.     *
  746.     *  The basic calculation used is to recognize that the arbitrary
  747.     *  axis vector (x, y, z), since it is of unit length, actually
  748.     *  represents the sines and cosines of the angles to rotate the
  749.     *  X-axis to the same orientation, with theta being the angle about
  750.     *  Z and phi the angle about Y (in the order described above)
  751.     *  as follows:
  752.     *
  753.     *  cos ( theta ) = x / sqrt ( 1 - z^2 )
  754.     *  sin ( theta ) = y / sqrt ( 1 - z^2 )
  755.     *
  756.     *  cos ( phi ) = sqrt ( 1 - z^2 )
  757.     *  sin ( phi ) = z
  758.     *
  759.     *  Note that cos ( phi ) can further be inserted to the above
  760.     *  formulas:
  761.     *
  762.     *  cos ( theta ) = x / cos ( phi )
  763.     *  sin ( theta ) = y / sin ( phi )
  764.     *
  765.     *  ...etc.  Because of those relations and the standard trigonometric
  766.     *  relations, it is pssible to reduce the transforms down to what
  767.     *  is used below.  It may be that any primary axis chosen will give the
  768.     *  same results (modulo a sign convention) using thie method.
  769.     *
  770.     *  Particularly nice is to notice that all divisions that might
  771.     *  have caused trouble when parallel to certain planes or
  772.     *  axis go away with care paid to reducing the expressions.
  773.     *  After checking, it does perform correctly under all cases, since
  774.     *  in all the cases of division where the denominator would have
  775.     *  been zero, the numerator would have been zero as well, giving
  776.     *  the expected result.
  777.     */
  778.  
  779.    xx = x * x;
  780.    yy = y * y;
  781.    zz = z * z;
  782.    xy = x * y;
  783.    yz = y * z;
  784.    zx = z * x;
  785.    xs = x * s;
  786.    ys = y * s;
  787.    zs = z * s;
  788.    one_c = 1.0F - c;
  789.  
  790.    M(0,0) = (one_c * xx) + c;
  791.    M(0,1) = (one_c * xy) - zs;
  792.    M(0,2) = (one_c * zx) + ys;
  793.    M(0,3) = 0.0F;
  794.  
  795.    M(1,0) = (one_c * xy) + zs;
  796.    M(1,1) = (one_c * yy) + c;
  797.    M(1,2) = (one_c * yz) - xs;
  798.    M(1,3) = 0.0F;
  799.  
  800.    M(2,0) = (one_c * zx) - ys;
  801.    M(2,1) = (one_c * yz) + xs;
  802.    M(2,2) = (one_c * zz) + c;
  803.    M(2,3) = 0.0F;
  804.  
  805.    M(3,0) = 0.0F;
  806.    M(3,1) = 0.0F;
  807.    M(3,2) = 0.0F;
  808.    M(3,3) = 1.0F;
  809.  
  810. #undef M
  811. }
  812.  
  813.  
  814.  
  815. void gl_Rotatef( GLcontext *ctx,
  816.          GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
  817. {
  818.    GLfloat m[16];
  819.    gl_rotation_matrix( angle, x, y, z, m );
  820.    gl_MultMatrixf( ctx, m );
  821. }
  822.  
  823.  
  824.  
  825. /*
  826.  * Execute a glScale call
  827.  */
  828. void gl_Scalef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z )
  829. {
  830.    GLfloat *m;
  831.  
  832.    if (INSIDE_BEGIN_END(ctx)) {
  833.       gl_error( ctx,  GL_INVALID_OPERATION, "glScale" );
  834.       return;
  835.    }
  836.    switch (ctx->Transform.MatrixMode) {
  837.       case GL_MODELVIEW:
  838.      m = ctx->ModelViewMatrix;
  839.      ctx->NewModelViewMatrix = GL_TRUE;
  840.      break;
  841.       case GL_PROJECTION:
  842.      m = ctx->ProjectionMatrix;
  843.      ctx->NewProjectionMatrix = GL_TRUE;
  844.      break;
  845.       case GL_TEXTURE:
  846.      {
  847.         GLuint texSet = ctx->Texture.CurrentTransformSet;
  848.         m = ctx->TextureMatrix[texSet];
  849.         ctx->NewTextureMatrix = GL_TRUE;
  850.      }
  851.      break;
  852.       default:
  853.      gl_problem(ctx, "Bad matrix mode in gl_Scalef");
  854.      return;
  855.    }
  856.    m[0] *= x;   m[4] *= y;   m[8]  *= z;
  857.    m[1] *= x;   m[5] *= y;   m[9]  *= z;
  858.    m[2] *= x;   m[6] *= y;   m[10] *= z;
  859.    m[3] *= x;   m[7] *= y;   m[11] *= z;
  860. }
  861.  
  862.  
  863.  
  864. /*
  865.  * Execute a glTranslate call
  866.  */
  867. void gl_Translatef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z )
  868. {
  869.    GLfloat *m;
  870.    if (INSIDE_BEGIN_END(ctx)) {
  871.       gl_error( ctx, GL_INVALID_OPERATION, "glTranslate" );
  872.       return;
  873.    }
  874.    switch (ctx->Transform.MatrixMode) {
  875.       case GL_MODELVIEW:
  876.      m = ctx->ModelViewMatrix;
  877.      ctx->NewModelViewMatrix = GL_TRUE;
  878.      break;
  879.       case GL_PROJECTION:
  880.      m = ctx->ProjectionMatrix;
  881.      ctx->NewProjectionMatrix = GL_TRUE;
  882.      break;
  883.       case GL_TEXTURE:
  884.      {
  885.         GLuint texSet = ctx->Texture.CurrentTransformSet;
  886.         m = ctx->TextureMatrix[texSet];
  887.         ctx->NewTextureMatrix = GL_TRUE;
  888.      }
  889.      break;
  890.       default:
  891.      gl_problem(ctx, "Bad matrix mode in gl_Translatef");
  892.      return;
  893.    }
  894.  
  895.    m[12] = m[0] * x + m[4] * y + m[8]  * z + m[12];
  896.    m[13] = m[1] * x + m[5] * y + m[9]  * z + m[13];
  897.    m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
  898.    m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
  899. }
  900.  
  901.  
  902.  
  903.  
  904. /*
  905.  * Define a new viewport and reallocate auxillary buffers if the size of
  906.  * the window (color buffer) has changed.
  907.  */
  908. void gl_Viewport( GLcontext *ctx,
  909.           GLint x, GLint y, GLsizei width, GLsizei height )
  910. {
  911.    if (width<0 || height<0) {
  912.       gl_error( ctx,  GL_INVALID_VALUE, "glViewport" );
  913.       return;
  914.    }
  915.    if (INSIDE_BEGIN_END(ctx)) {
  916.       gl_error( ctx,  GL_INVALID_OPERATION, "glViewport" );
  917.       return;
  918.    }
  919.  
  920.    /* clamp width, and height to implementation dependent range */
  921.    width  = CLAMP( width,  1, MAX_WIDTH );
  922.    height = CLAMP( height, 1, MAX_HEIGHT );
  923.  
  924.    /* Save viewport */
  925.    ctx->Viewport.X = x;
  926.    ctx->Viewport.Width = width;
  927.    ctx->Viewport.Y = y;
  928.    ctx->Viewport.Height = height;
  929.  
  930.    /* compute scale and bias values */
  931.    ctx->Viewport.Sx = (GLfloat) width / 2.0F;
  932.    ctx->Viewport.Tx = ctx->Viewport.Sx + x;
  933.    ctx->Viewport.Sy = (GLfloat) height / 2.0F;
  934.    ctx->Viewport.Ty = ctx->Viewport.Sy + y;
  935.  
  936.    ctx->NewState |= NEW_ALL;   /* just to be safe */
  937.  
  938.    /* Check if window/buffer has been resized and if so, reallocate the
  939.     * ancillary buffers.
  940.     */
  941.    gl_ResizeBuffersMESA(ctx);
  942.  
  943.    if (ctx->Driver.Viewport) {
  944.       (*ctx->Driver.Viewport)( ctx, x, y, width, height );
  945.    }
  946. }
  947.  
  948.  
  949.  
  950. void gl_DepthRange( GLcontext *ctx, GLclampd nearval, GLclampd farval )
  951. {
  952.    /*
  953.     * nearval - specifies mapping of the near clipping plane to window
  954.     *   coordinates, default is 0
  955.     * farval - specifies mapping of the far clipping plane to window
  956.     *   coordinates, default is 1
  957.     *
  958.     * After clipping and div by w, z coords are in -1.0 to 1.0,
  959.     * corresponding to near and far clipping planes.  glDepthRange
  960.     * specifies a linear mapping of the normalized z coords in
  961.     * this range to window z coords.
  962.     */
  963.    GLfloat n, f;
  964.  
  965.    if (INSIDE_BEGIN_END(ctx)) {
  966.       gl_error( ctx, GL_INVALID_OPERATION, "glDepthRange" );
  967.       return;
  968.    }
  969.  
  970.    n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
  971.    f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
  972.  
  973.    ctx->Viewport.Near = n;
  974.    ctx->Viewport.Far = f;
  975.    ctx->Viewport.Sz = DEPTH_SCALE * ((f - n) / 2.0);
  976.    ctx->Viewport.Tz = DEPTH_SCALE * ((f - n) / 2.0 + n);
  977.  
  978.    if (ctx->Driver.DepthRange) {
  979.       (*ctx->Driver.DepthRange)( ctx, nearval, farval );
  980.    }
  981. }
  982.